package com.huli.controller;

import com.huli.pojo.SysOrganization;
import com.huli.service.OrganizationUserService;
import com.huli.util.RedisClient;
import com.huli.util.Result;
import com.huli.util.qiniuyun.QiniuUtils;
import com.huli.vo.OrganizationUserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/organizationUser")
public class OrganizationUserController {
    // 注入OrganizationUserService
    @Autowired
    private OrganizationUserService organizationUserService;

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam(value = "imgFile") MultipartFile imgFile) {
        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index);
        String fileName = UUID.randomUUID().toString() + extention;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }
        return Result.success("上传成功", fileName);
    }

    // 根据OrganizationUserVo查询组织用户列表
    @RequestMapping("/organizationUserList")
    public Result organizationUserList2(OrganizationUserVo organizationUserVo){
            // 调用OrganizationUserService的organizationUserList方法查询组织用户列表
        OrganizationUserVo organizationUserVoList = organizationUserService.organizationUserList(organizationUserVo);
        return Result.success(organizationUserVoList);
    }

    //根据orgName模糊查询
    @RequestMapping("/organizationListByOrgName")
    public List<SysOrganization> organizationUserListByOrgName(String orgName){
        List<SysOrganization> organizationUserListByOrgName = organizationUserService.organizationUserListByOrgName(orgName);
        return organizationUserListByOrgName;
    }

    // 根据id删除组织用户
    @RequestMapping("/deleteOrganization")
    public Result deleteOrganizationUser(Long id){
        try {
            organizationUserService.deleteOrganizationUser(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    //新增机构
    @RequestMapping("/addOrganization")
    public Result addOrganization(SysOrganization sysOrganization){
        try {
            organizationUserService.addOrganization(sysOrganization);
            return Result.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }
    }

    /**
     * 修改机构信息
     * @param sysOrganization
     * @return
     */
    @RequestMapping("/updateOrganization")
    public Result updateOrganization(SysOrganization sysOrganization){
        try {
            organizationUserService.updateOrganization(sysOrganization);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    /**
     * 根据id查询机构信息
     * @param id
     * @return
     */
    @RequestMapping("/organizationById")
    public Result organizationById(Long id){
        SysOrganization organizationById = organizationUserService.selectOrganizationById(id);
        return Result.success(organizationById);
    }

    /**
     * 查询所有机构
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        List<SysOrganization> all = organizationUserService.findAll();
        return Result.success(all);
    }
}
